'''
Created on 2010-02-09

@author: tomek
'''
#from win import PyScreenshot

import gtk
import ConfigParser

class winFtp(gtk.Window):
    def __init__(self, pyscreenshot):
        super(winFtp, self).__init__()
        self.set_modal(True)
        self.set_title("FTP")
        self.set_size_request(350, -1)
        self.set_position(gtk.WIN_POS_CENTER)
        
        self.config_file = pyscreenshot.app_config_dir + '/ftp.ini'

        self.config = pyscreenshot.configFtp
        
        self.create_window()
        
        self.show_all()
        
    def create_window(self):
        
        
        box = gtk.VBox()
        self.add(box)
        
        labelDscr = gtk.Label('F T P - Settings')
        hbox0 = gtk.HBox()
        hbox0.pack_start(labelDscr,  True, True, 0)

        labelName = gtk.Label('Name')
        labelName.set_size_request(100, -1)
        self.entryName = gtk.Entry()
        if self.config.has_option("ftp", "name"):
            self.entryName.set_text(self.config.get("ftp", "name"))
        hbox1 = gtk.HBox()
        hbox1.pack_start(labelName,  False, False, 0)
        hbox1.pack_start(self.entryName,  True, True, 0)
        
        labelHost = gtk.Label('Host')
        labelHost.set_size_request(100, -1)
        self.entryHost = gtk.Entry()
        if self.config.has_option("ftp", "host"):
            self.entryHost.set_text(self.config.get("ftp", "host"))
        hbox2 = gtk.HBox()
        hbox2.pack_start(labelHost,  False, False, 0)
        hbox2.pack_start(self.entryHost,  True, True, 0)
        
        labelUser = gtk.Label('User')
        labelUser.set_size_request(100, -1)
        self.entryUser = gtk.Entry()
        if self.config.has_option("ftp", "user"):
            self.entryUser.set_text( str( self.config.get("ftp", "user") ) )
        hbox3 = gtk.HBox()
        hbox3.pack_start(labelUser,  False, False, 0)
        hbox3.pack_start(self.entryUser,  True, True, 0)
        
        labelPass = gtk.Label('Pass')
        labelPass.set_size_request(100, -1)
        self.entryPass = gtk.Entry()
        if self.config.has_option("ftp", "pass"):
            self.entryPass.set_text(self.config.get("ftp", "pass"))
        hbox4 = gtk.HBox()
        hbox4.pack_start(labelPass, False, False, 0)
        hbox4.pack_start(self.entryPass,  True, True, 0)
       
        labelPath = gtk.Label('Path')
        labelPath.set_size_request(100, -1)
        self.entryPath = gtk.Entry()
        if self.config.has_option("ftp", "path"):
            self.entryPath.set_text(self.config.get("ftp", "path"))
        hbox7 = gtk.HBox()
        hbox7.pack_start(labelPath, False, False, 0)
        hbox7.pack_start(self.entryPath,  True, True, 0)
        
        labelUrl = gtk.Label('Url')
        labelUrl.set_size_request(100, -1)
        self.entryUrl = gtk.Entry()
        if self.config.has_option("ftp", "url"):
            self.entryUrl.set_text(self.config.get("ftp", "url"))
        hbox5 = gtk.HBox()
        hbox5.pack_start(labelUrl, False, False, 0)
        hbox5.pack_start(self.entryUrl,  True, True, 0)
        
        buttonSave = gtk.Button("Save")
        buttonSave.connect("clicked", self.save_ftp_config)
        hbox6 = gtk.HBox()
        hbox6.pack_start(buttonSave,  True, True, 0)
        
        box.pack_start( hbox0, False, False, 0 )
        box.pack_start( hbox1, False, False, 0 )
        box.pack_start( hbox2, False, False, 0 )
        box.pack_start( hbox3, False, False, 0 )
        box.pack_start( hbox4, False, False, 0 )
        box.pack_start( hbox7, False, False, 0 )
        box.pack_start( hbox5, False, False, 0 )
        box.pack_start( hbox6, False, False, 0 )
        
    def get_ftp_config(self, widget):
        pass
    
    
    def save_ftp_config(self, widget):
        try:
            self.config.set('ftp', 'name', self.entryName.get_text())
        except ConfigParser.NoSectionError:
            self.config.add_section('ftp')
            self.config.set('ftp', 'name', self.entryName.get_text())
            
        self.config.set('ftp', 'host', self.entryHost.get_text())
        self.config.set('ftp', 'user', self.entryUser.get_text())
        self.config.set('ftp', 'pass', self.entryPass.get_text())
        self.config.set('ftp', 'url', self.entryUrl.get_text())
        self.config.set('ftp', 'path', self.entryPath.get_text())
        
        self.config.write(open(self.config_file, 'w'))
        